
--Xu ly tinh--
delete  from [vncom].[dbo].[TBProvinces]

declare @name nvarchar(100)
declare @long float
declare @lat float

declare _cursor cursor fast_forward for
select [ProvinceName],[Longitude],[Latitude] from [DnnetDB].[dbo].[TBProvince]

open _cursor
fetch next from _cursor 
into @name, @long, @lat

while @@fetch_status = 0
begin

	insert into [vncom].[dbo].[TBProvinces](ProvinceName,Longitude,Latitude) values(@name,@long,@lat)		

	-- fetch next the cursor
	fetch next from _cursor
	into @name, @long, @lat
end
 
close _cursor
deallocate _cursor
go


--Xu ly huyen--
delete  from [vncom].[dbo].[TBDistricts]

declare @dname nvarchar(255)
declare @pname nvarchar(100)
declare @idp int
declare @newid int
declare _cursor1 cursor fast_forward for
select [DistrictName], [IDProvince] from [DnnetDB].[dbo].[TBDistrict]

open _cursor1
fetch next from _cursor1
into @dname, @idp

while @@fetch_status = 0
begin
	select @pname = [ProvinceName] from [DnnetDB].[dbo].[TBProvince] where [IDProvince]=@idp
	select @newid = [ProvinceID] from [vncom].[dbo].[TBProvinces] where [ProvinceName]=@pname
	insert into [vncom].[dbo].[TBDistricts] (DistrictName, ProvinceID) values(@dname, @newid)		

	-- fetch next the cursor
	fetch next from _cursor1
	into @dname, @idp
end
 
close _cursor1
deallocate _cursor1
go
